set(MODULE_NAME "js_bindings_generator")
set(OPENCV_MODULE_IS_PART_OF_WORLD FALSE)
ocv_add_module(${MODULE_NAME} INTERNAL)

set(OPENCV_JS_BINDINGS_DIR "${CMAKE_CURRENT_BINARY_DIR}" CACHE INTERNAL "")
file(REMOVE_RECURSE "${OPENCV_JS_BINDINGS_DIR}/gen")
file(MAKE_DIRECTORY "${OPENCV_JS_BINDINGS_DIR}/gen")
file(REMOVE "${OPENCV_DEPHELPER}/gen_opencv_js_source")  # force re-run after CMake

# This file is included from a subdirectory
set(JS_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/..")
include(${JS_SOURCE_DIR}/common.cmake)  # fill OPENCV_JS_MODULES

set(opencv_hdrs "")
foreach(m ${OPENCV_JS_MODULES})
  list(APPEND opencv_hdrs ${OPENCV_MODULE_${m}_HEADERS})
endforeach(m)

# header blacklist
ocv_list_filterout(opencv_hdrs "modules/.*.h$")
ocv_list_filterout(opencv_hdrs "modules/core/include/opencv2/core/fast_math.hpp")
ocv_list_filterout(opencv_hdrs "modules/core/.*/cuda")
ocv_list_filterout(opencv_hdrs "modules/core/.*/opencl")
ocv_list_filterout(opencv_hdrs "modules/core/include/opencv2/core/opengl.hpp")
ocv_list_filterout(opencv_hdrs "modules/core/include/opencv2/core/ocl.hpp")
ocv_list_filterout(opencv_hdrs "modules/cuda.*")
ocv_list_filterout(opencv_hdrs "modules/cudev")
ocv_list_filterout(opencv_hdrs "modules/core/.*/hal/")
ocv_list_filterout(opencv_hdrs "modules/.*/detection_based_tracker.hpp") # Conditional compilation
ocv_list_filterout(opencv_hdrs "modules/core/include/opencv2/core/utils/*.private.*")
ocv_list_filterout(opencv_hdrs "modules/core/include/opencv2/core/utils/instrumentation.hpp")
ocv_list_filterout(opencv_hdrs "modules/core/include/opencv2/core/utils/trace*")

set(config_json_headers_list "")
foreach(header IN LISTS opencv_hdrs)
  if(NOT config_json_headers_list STREQUAL "")
    set(config_json_headers_list "${config_json_headers_list},\n\"${header}\"")
  else()
    set(config_json_headers_list "\"${header}\"")
  endif()
endforeach()

set(bindings_cpp "${OPENCV_JS_BINDINGS_DIR}/gen/bindings.cpp")

set(scripts_hdr_parser "${JS_SOURCE_DIR}/../python/src2/hdr_parser.py")

if(DEFINED ENV{OPENCV_JS_WHITELIST})
  set(OPENCV_JS_WHITELIST_FILE "$ENV{OPENCV_JS_WHITELIST}")
  message(STATUS "Use white list from environment ${OPENCV_JS_WHITELIST_FILE}")
else()
  #generate white list from modules/<module_name>/misc/js/whitelist.json
  set(OPENCV_JS_WHITELIST_FILE "${CMAKE_CURRENT_BINARY_DIR}/whitelist.json")
  foreach(m in ${OPENCV_JS_MODULES})
    set(js_whitelist "${OPENCV_MODULE_${m}_LOCATION}/misc/js/gen_dict.json")
    if (EXISTS "${js_whitelist}")
      file(READ "${js_whitelist}" whitelist_content)
      list(APPEND OPENCV_JS_WHITELIST_CONTENT  "\"${m}\": ${whitelist_content}")
    endif()
  endforeach(m)
  string(REPLACE ";" ", \n" OPENCV_JS_WHITELIST_CONTENT_STRING "${OPENCV_JS_WHITELIST_CONTENT}")
  set(OPENCV_JS_WHITELIST_CONTENT_STRING "{\n${OPENCV_JS_WHITELIST_CONTENT_STRING}}\n")
  ocv_update_file("${OPENCV_JS_WHITELIST_FILE}" "${OPENCV_JS_WHITELIST_CONTENT_STRING}")
  message(STATUS "Use autogenerated whitelist ${OPENCV_JS_WHITELIST_FILE}")
endif()

include("${OpenCV_SOURCE_DIR}/cmake/OpenCVBindingsPreprocessorDefinitions.cmake")
ocv_bindings_generator_populate_preprocessor_definitions(
  OPENCV_MODULES_BUILD
  opencv_preprocessor_defs
)

set(__config_str
"{
  \"headers\": [
${config_json_headers_list}
  ],
  \"preprocessor_definitions\": {
${opencv_preprocessor_defs}
  },
  \"core_bindings_file_path\": \"${JS_SOURCE_DIR}/src/core_bindings.cpp\"
}")
set(JSON_CONFIG_FILE_PATH "${CMAKE_CURRENT_BINARY_DIR}/gen_js_config.json")
if(EXISTS "${JSON_CONFIG_FILE_PATH}")
  file(READ "${JSON_CONFIG_FILE_PATH}" __content)
else()
  set(__content "")
endif()
if(NOT "${__content}" STREQUAL "${__config_str}")
  file(WRITE "${JSON_CONFIG_FILE_PATH}" "${__config_str}")
endif()
unset(__config_str)

add_custom_command(
  OUTPUT ${bindings_cpp} "${OPENCV_DEPHELPER}/gen_opencv_js_source"
  COMMAND
      ${PYTHON_DEFAULT_EXECUTABLE}
      "${CMAKE_CURRENT_SOURCE_DIR}/embindgen.py"
      --parser "${scripts_hdr_parser}"
      --output_file "${bindings_cpp}"
      --config "${JSON_CONFIG_FILE_PATH}"
      --whitelist "${OPENCV_JS_WHITELIST_FILE}"
  COMMAND
      ${CMAKE_COMMAND} -E touch "${OPENCV_DEPHELPER}/gen_opencv_js_source"
  WORKING_DIRECTORY
      "${CMAKE_CURRENT_BINARY_DIR}/gen"
  DEPENDS
      ${JS_SOURCE_DIR}/src/core_bindings.cpp
      ${CMAKE_CURRENT_SOURCE_DIR}/embindgen.py
      ${CMAKE_CURRENT_SOURCE_DIR}/templates.py
      ${JSON_CONFIG_FILE_PATH}
      "${OPENCV_JS_WHITELIST_FILE}"
      ${scripts_hdr_parser}
      #(not needed - generated by CMake) ${CMAKE_CURRENT_BINARY_DIR}/headers.txt
      ${opencv_hdrs}
  COMMENT "Generate source files for JavaScript bindings"
)

add_custom_target(gen_opencv_js_source
  # excluded from all: ALL
  DEPENDS ${bindings_cpp} "${OPENCV_DEPHELPER}/gen_opencv_js_source"
  SOURCES
      ${JS_SOURCE_DIR}/src/core_bindings.cpp
      ${CMAKE_CURRENT_SOURCE_DIR}/embindgen.py
      ${CMAKE_CURRENT_SOURCE_DIR}/templates.py
)
